package com.originskyseed.platform.cms.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.cms.mapper.ProductInfoTemplateMapper;
import com.originskyseed.platform.cms.model.ProductInfoTemplate;
import com.originskyseed.platform.cms.service.IProductInfoTemplateService;
import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;

/**
 * 产品模板信息表Service接口实现类（OSI_PRODUCT_INFO_TEMPLATE）
 *
 * @author Tangwanghui
 */
@DubboService
@Component("productInfoTemplateService")
public class ProductInfoTemplateServiceImpl implements IProductInfoTemplateService {

	@Resource
	private ProductInfoTemplateMapper productInfoTemplateMapper;

	/**
	 * 删除记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.productInfoTemplateMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 *
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.productInfoTemplateMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(ProductInfoTemplate record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.productInfoTemplateMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(ProductInfoTemplate record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.productInfoTemplateMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(ProductInfoTemplate record) throws Exception {
		record.setUpdateTime(new Date());
		return this.productInfoTemplateMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(ProductInfoTemplate record) throws Exception {
		record.setUpdateTime(new Date());
		return this.productInfoTemplateMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 *
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public ProductInfoTemplate selectByPrimaryKey(String id) throws Exception {
		return this.productInfoTemplateMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 *
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<ProductInfoTemplate> selectList(ProductInfoTemplate record) throws Exception {
		return this.productInfoTemplateMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 *
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<ProductInfoTemplate> selectListByPage(Map<String, Object> params, BasePage<ProductInfoTemplate> page) throws Exception {
		params.put("page", page);
		List<ProductInfoTemplate> list = this.productInfoTemplateMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 查询记录
	 *
	 * @param userId
	 * @return
	 * @throws Exception
	 */
	@Override
	public ProductInfoTemplate selectProductInfoTemplateByUserId(String userId) throws Exception {
		return this.productInfoTemplateMapper.selectProductInfoTemplateByUserId(userId);
	}

	/**
	 * 修改默认模板
	 *
	 * @param id
	 * @param userId
	 * @param isDefault
	 * @return
	 * @throws Exception
	 */
	public int updateIsDefault(String id,String userId,Integer isDefault) throws Exception{
		this.productInfoTemplateMapper.updateNotDefault(userId);
		ProductInfoTemplate productInfoTemplate = new ProductInfoTemplate();
		productInfoTemplate.setId(id);
		productInfoTemplate.setUserId(userId);
		productInfoTemplate.setIsDefault(1);
		return this.updateByPrimaryKeySelective(productInfoTemplate);
	}
}
